package eclipse.client.forms;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import eclipse.*;
import eclipse.client.*;
import eclipse.lib.sockets.*;
/**
 * The MouseHandler class handles all mouse functions for any menu. This class is not intended to be used outside of the Application.
 *
 */
public class MouseHandler implements MouseListener
{
	private int MENU_STATE;
	/**	Launches the MouseHandler with a specific listening menu. */
	public MouseHandler(int iMenu_State)
	{
		MENU_STATE = iMenu_State;
	}
	/** Launches the MouseHandler with a specific listening menu and adds it */
	public MouseHandler(int iMenu_State, Container dstSource)
	{
		dstSource.addMouseListener(this);
		MENU_STATE = iMenu_State;
	}
	/** Launches the MouseHandler with a specific listening menu and adds it */
	public MouseHandler(Container dstSource, int iMenu_State)
	{
		dstSource.addMouseListener(this);
		MENU_STATE = iMenu_State;
	}
	public void mouseClicked(MouseEvent e)
	{	
		if(MENU_STATE == Constants.MENUSTATE_MAINMENU)
			MainMenuMouseEvents(e);
		else if(MENU_STATE == Constants.MENUSTATE_LOGINMENU)
			LoginMenuMouseEvents(e);
		else if(MENU_STATE == Constants.MENUSTATE_NEWACCOUNTMENU)
			NewAccountMenuMouseEvents(e);
	}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	private void LoginMenuMouseEvents(MouseEvent e)
	{
		int x = e.getX();
		int y = e.getY();
		if(x > Database.LOGINEXITX &&
			x < Database.LOGINEXITW + Database.LOGINEXITX &&
			y > Database.LOGINEXITY &&
			y < Database.LOGINEXITH + Database.LOGINEXITY
		)
		{				 
			Database.Login.setVisible(false);
			Database.MainMenu.setVisible(true);
			return;
		}		
		if(x > Database.LOGINLOGINX &&
			x < Database.LOGINLOGINX + Database.LOGINLOGINW &&
			y > Database.LOGINLOGINY &&
			y < Database.LOGINLOGINY + Database.LOGINLOGINH
		)
		{
			if(Database.AllDataReceived)
			{
				Database.USERNAME = Database.Login.txtUName.getText();
				Database.PASSWORD = new String(Database.Login.txtPWord.getPassword());				
				if(Database.USERNAME.length() < 6)
				{
					JOptionPane.showMessageDialog(null, "Your username must be at least three characters in length.", "Login Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if(Database.PASSWORD.length() < 6)
				{
					JOptionPane.showMessageDialog(null, "Your password must be at least three characters in length.", "Login Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				ClientDataHandle.requestLogin();				
			}
			else
				JOptionPane.showMessageDialog(null, "Please try again, attempting to retrieve all data from the server...");
			return;
		}
		System.out.println("X: " + x + "Y: " + y);
	}
	private void MainMenuMouseEvents(MouseEvent e)
	{
		int x = e.getX();
		int y = e.getY();
		if(x > Database.MAINMENUEXITX &&
			x < Database.MAINMENUEXITW + Database.MAINMENUEXITX && 
			y > Database.MAINMENUEXITY &&
			y < Database.MAINMENUEXITH + Database.MAINMENUEXITY)
		{	
			System.exit(0);
			return;
		}
		if(x > Database.MAINMENULOGINX &&
			x < Database.MAINMENULOGINW + Database.MAINMENULOGINX &&
			y > Database.MAINMENULOGINY &&
			y < Database.MAINMENULOGINH + Database.MAINMENULOGINY
		)
		{
			Database.Login.setVisible(true);
			Database.MainMenu.setVisible(false);
			return;
		}
		if(x > Database.MAINMENUNEWACCOUNTX &&
			x < Database.MAINMENUNEWACCOUNTW + Database.MAINMENUNEWACCOUNTX &&
			y > Database.MAINMENUNEWACCOUNTY &&
			y < Database.MAINMENUNEWACCOUNTY + Database.MAINMENUNEWACCOUNTH
		)	
		{
			Database.NewAccount.setVisible(true);
			Database.MainMenu.setVisible(false);
			return;
		}
		System.out.println("X: " + x + "Y: " + y);
	}
	private void NewAccountMenuMouseEvents(MouseEvent e)
	{
		int x = e.getX();
		int y = e.getY();
		String password, confword, username;				
		
		if(x > Database.NEWACCOUNTCONFX &&
			x < Database.NEWACCOUNTCONFW + Database.NEWACCOUNTCONFX &&
			y > Database.NEWACCOUNTCONFY &&
			y < Database.NEWACCOUNTCONFH + Database.NEWACCOUNTCONFY
		)
		{
			password = new String(Database.NewAccount.txtPWord.getPassword());
			confword = new String(Database.NewAccount.txtCWord.getPassword());
			username = Database.NewAccount.txtUName.getText();
			if(!password.equals(confword))
			{
				JOptionPane.showMessageDialog(null, "Please confirm that you passwords match!");
				return;
			}
			if(password.length() < 5)
			{
				JOptionPane.showMessageDialog(null, "Your password has to have a minimum of 5 characters!");
				return;
			}
			if(username.length() < 3)
			{
				JOptionPane.showMessageDialog(null, "Your username has to have a minimum of 3 characters!");
				return;
			}
			Database.USERNAME = username;
			Database.PASSWORD = password;
			eclipse.lib.sockets.ClientDataHandle.requestNewAccount();
			return;
		}
		if(x > Database.NEWACCOUNTEXITX &&
			x < Database.NEWACCOUNTEXITW + Database.NEWACCOUNTEXITX &&
			y > Database.NEWACCOUNTEXITY &&
			y < Database.NEWACCOUNTEXITH + Database.NEWACCOUNTEXITY
		)
		{
			Database.NewAccount.setVisible(false);
			Database.MainMenu.setVisible(true);
			return;
		}
			
		System.out.println("X: " + x + "Y: " + y);
	}
}
